var p []int

func minMalwareSpread(graph [][]int, initial []int) int {
	n := len(graph)
	p = make([]int, n)
	size := make([]int, n)
	for i := 0; i < n; i++ {
		p[i] = i
		size[i] = 1
	}
	for i := 0; i < n; i++ {
		for j := i + 1; j < n; j++ {
			if graph[i][j] == 1 {
				pa, pb := find(i), find(j)
				if pa == pb {
					continue
				}
				p[pa] = pb
				size[pb] += size[pa]
			}
		}
	}
	mi := 400
	res := initial[0]
	sort.Ints(initial)
	for i := 0; i < len(initial); i++ {
		t := 0
		s := make(map[int]bool)
		for j := 0; j < len(initial); j++ {
			if i == j {
				continue
			}
			if s[find(initial[j])] {
				continue
			}
			s[find(initial[j])] = true
			t += size[find(initial[j])]
		}
		if mi > t {
			mi = t
			res = initial[i]
		}
	}
	return res
}

func find(x int) int {
	if p[x] != x {
		p[x] = find(p[x])
	}
	return p[x]
}